<template>
  <view v-if="goodsDetail.goods_name">
    <!-- 轮播图区域 -->
    <swiper 
      :indicator-dots="true" 
      :autoplay="true" 
      :interval="3000" 
      :duration="1000"
      class='swiper_box'>
      <swiper-item v-for="(item,index) in goodsDetail.pics" :key="index">
        <image :src="item.pics_big" @click="lookImg(item,index)"></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息展示区域 -->
    <view class="goods_deatil_box" >
      <!-- 商品价格 -->
      <view class="deatil_price">￥{{toFixed(goodsDetail.goods_price)}}</view>
      <!-- 商品信息主体区域 -->
      <view class="deatil_body">
        <!-- 商品标题 -->
        <view class="deatil_title">{{goodsDetail.goods_name}}</view>
        <view class="deatil_icons">
          <uni-icons type="star" size="22" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 优惠区域 -->
      <view class="deatil_body_bootm">快递：免运费</view>
    </view>
    <rich-text class="rich_text" :nodes="goodsDetail.goods_introduce"></rich-text>
    <view class="deatil_bottom_btn">
      <uni-goods-nav 
        :fill="true" 
        :options="options" 
        :button-group="buttonGroup"
        @click="leftBtn"
        @buttonClick="rightButton"
         />
    </view>
  </view>
</template>

<script>
  import { mapGetters } from 'vuex'
  export default {
    data() {
      return {
        goods_id:null,
        goodsDetail:{},
        // 左侧按钮组的配置对象
        options: [{
          icon: 'shop',
          text: '店铺'
          },{
          icon: 'cart',
          text: '购物车',
          info: 0 || ''
        }],
        // 右侧按钮组的配置对象
        buttonGroup: [{
          text: '加入购物车',
          backgroundColor: '#ff0000',
          color: '#fff'
          },{
          text: '立即购买',
          backgroundColor: '#ffa200',
          color: '#fff'
        }]
      };
    },
    computed: {
      ...mapGetters('Cart',['toCart'])
    },
    watch: {
      // 页面初始化立刻执行一次
      toCart:{
        handler(val) {
          this.options[1].info = val
        },
        immediate:true
      }
    },
    onLoad(option) {
      this.goods_id = option.goods_id
      this.getGoodsDetail()
    },
    methods:{
      async getGoodsDetail() {
        const { data: res } = await uni.$http.get('/api/public/v1/goods/detail?goods_id=' + this.goods_id)
        if (res.meta.status !== 200) return uni.$showMsg()
        // 使用字符串的 replace() 方法，为 img 标签添加行内的 style 样式，从而解决图片底部空白间隙的问题
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ').replace(/webp/g, 'jpg')
        // 为 data 中的数据赋值
        this.goodsDetail = res.message
      },
      lookImg(item,index) {
        // 调用 uni.previewImage() 方法预览图片
        uni.previewImage({
          // 预览时，默认显示图片的索引
          current: index,
          // 所有图片 url 地址的数组
          urls: this.goodsDetail.pics.map((x)=>x.pics_big)
        })
      },
      toFixed(price) {
        return Number(price).toFixed(2)
      },
      // 商品导航左侧按钮
      leftBtn(e) {
        if(e.content.text === '购物车') {
          uni.switchTab({
            url:'/pages/cart/cart'
          })
        }
      },
      // 商品导航右侧按钮
     rightButton(e) {
       if(e.content.text === '加入购物车') {
          const goods = {
            goods_id:this.goodsDetail.goods_id,
            goods_name:this.goodsDetail.goods_name,
            goods_price:this.goodsDetail.goods_price,
            goods_small_logo:this.goodsDetail.goods_small_logo,
            goods_count : 1,
            goods_state : true 
          }
          // 把商品信息存到vuex中
          this.$store.commit('Cart/getCart',goods)
          // 再把vuex的商品信息持久化到本地
          this.$store.commit('Cart/saveToStorage') 
       } else{
         uni.$showMsg("没钱钱惹！v我50继续开发！")
       }
     }
    }
  }
</script>

<style lang="scss">
.swiper_box {
  height: 750rpx;
  margin-bottom: 5px;
  
  image {
    width: 100%;
    height: 100%;
  }
}
 .goods_deatil_box {
    padding: 10px 10px;
    padding-right: 0;
    .deatil_price {
      margin-bottom: 12px;
      color: #ff0000;
      font-weight: bold;
    }
    .deatil_body {
      display: flex;
      
      .deatil_title {
        width: 85%;
        font-size: 13px;
        padding-right: 5px;
        border-right: 1px solid #f0f0f0;
      }
      .deatil_icons {
        width: 15%;
        display: flex;
        flex-direction:column;
        justify-content: center;
        align-items: center;
        
        text {
          font-size: 13px;
        }
      }
    }
    .deatil_body_bootm {
      font-size: 13px;
      color: gray;
      margin: 10px 0
    }
  }
  .deatil_bottom_btn {
    position: sticky;
    bottom: 0;
    left: 0;
    width: 100%;
  }
</style>
